#include "SceneManager.h"
#include "RealTimeController.h"
#include "MainMenu.h"
#include "StageInfo.h"
#include "ScoreSystem.h"
#include "CatchTheGarbage.h"
#include "SaveTheDam.h"
#include "SuckTheMinions.h"
#include "SavingWater.h"
#include "EscapeRune.h"

using namespace Pulpy;
using namespace std;
bool fullscreen = false;
int _height = 600;
bool _SinglePlayer = false;

#ifndef _DEBUG
#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup")
#endif

void RenderScene()
{
	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); 

	SceneManager::getInstance().drawScenes();

	if(RealTimeController::getInstance().isUpdate())
	{
		SceneManager::getInstance().updateScenes();
	}


	glutSwapBuffers();
	glutPostRedisplay();

}

void Resize( int width, int height )
{
	glViewport ( 0, 0, width, height );
	_height = height;
	glMatrixMode ( GL_PROJECTION );
	glLoadIdentity();
	//gluOrtho2D(0,width,0,height);
	//glOrtho(0,width,0,height,0,1);
	glOrtho( 0, width , height, 0, -1, 1 );   
	//gluPerspective( 45, (float)(width)/(float)(height), 0.1f, 1000 );
	glMatrixMode ( GL_MODELVIEW );
	glLoadIdentity ();
}
void NormalKeys( unsigned char key, int x, int y )
{
	SceneManager::getInstance().handleKeyboard(key, x, y);
}

void NormalKeysUp( unsigned char key, int x, int y )
{
	SceneManager::getInstance().handleKeyboardUp(key,x,y);
}

void SpecialKeys( int key, int x, int y )
{
	SceneManager::getInstance().handleKeyboardSpecial(key, x, y);
	glutPostRedisplay();
}
void ProcessMouseClick( int button, int state, int x, int y )
{
	SceneManager::getInstance().handleMouseClick(button, state, x, _height-y);
}

void ProcessMouseMove( int x, int y ) 
{
	SceneManager::getInstance().handleMouseMove(x, _height-y);
}

void InitGL()
{
	//glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
	//glEnable(GL_DEPTH_TEST);
	//glDepthFunc(GL_LEQUAL); 
	//glClearDepth( 1.0 );

	
	//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	//glEnable(GL_BLEND);
	//glAlphaFunc(GL_GREATER,0.1);
	//glEnable(GL_ALPHA_TEST);
	//glShadeModel(GL_SMOOTH); 
	//glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); 
	RealTimeController::getInstance().setFPSLimit(60.0f);

	glEnable(GL_TEXTURE_2D);
	glEnable(GL_DEPTH_TEST);
	glMatrixMode(GL_PROJECTION); 
	glLoadIdentity(); 
	glOrtho(0,800,600,0,-1,1);
	glMatrixMode ( GL_MODELVIEW );
	glLoadIdentity();
	
	//glOrtho( 0, 800 , 600, 0, -1, 1 ); 
	SceneManager::getInstance().addScene("Main_Menu", new MainMenu);
	SceneManager::getInstance().addScene("Stage_Info", new StageInfo);
	SceneManager::getInstance().addScene("Catch_Garbage", new CatchTheGarbage);
	SceneManager::getInstance().addScene("Save_Dam", new SaveTheDam);
	SceneManager::getInstance().addScene("Suck_Minions", new SuckTheMinions);
	SceneManager::getInstance().addScene("Saving_Water", new SavingWater);
	SceneManager::getInstance().addScene("EscapeRune", new EscapeRune);
	SceneManager::getInstance().addScene("High_Score", new HighScorePage);
	SceneManager::getInstance().addScene("Score_Board", new ScoreBoard);
	//SceneManager::getInstance().addScene("Game", new GameScene);
	SceneManager::getInstance().changeScene("Main_Menu");
}

void main(int argc, char *argv[] )
{
	glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
	glutInit( &argc, argv ); 
	glutInitWindowSize(800,600);
	if (fullscreen) { 
		glutGameModeString("800x600:16@60"); 
		glutEnterGameMode(); 
	} else glutCreateWindow( "SP3" ); 
	InitGL();
	glutIdleFunc( RenderScene ); 
	glutReshapeFunc( Resize ); 
	glutDisplayFunc( RenderScene ); 
	glutKeyboardFunc( NormalKeys ); 
	glutKeyboardUpFunc(NormalKeysUp);
	glutSpecialFunc( SpecialKeys ); 
	glutMouseFunc( ProcessMouseClick ); 
	glutPassiveMotionFunc( ProcessMouseMove ); 
	
	glutMainLoop(); 
}

